package me.nereo.multi_image_selector;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import me.nereo.multi_image_selector.adapter.FolderAdapter;
import me.nereo.multi_image_selector.adapter.ImageGridAdapter;
import me.nereo.multi_image_selector.bean.Folder;
import me.nereo.multi_image_selector.bean.Image;
import me.nereo.multi_image_selector.utils.FileUtils;
import me.nereo.multi_image_selector.utils.ScreenUtils;

/**
 * 图片选择Fragment
 * Created by Nereo on 2015/4/7.
 */
public class MultiImageSelectorFragment extends Fragment {
    private static final int PREVIEW_VIDEO = 110;
    private final String[] IMAGE_PROJECTION = {
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_ADDED,
            MediaStore.Images.ImageColumns.MIME_TYPE,
            MediaStore.Images.ImageColumns.SIZE,
            MediaStore.Images.ImageColumns._ID};

    private final String[] VIDEO_PROJECTION = {
            MediaStore.Video.VideoColumns.DATA,
            MediaStore.Video.VideoColumns.DISPLAY_NAME,
            MediaStore.Video.VideoColumns.DATE_ADDED,
            MediaStore.Video.VideoColumns.MIME_TYPE,
            MediaStore.Video.VideoColumns.DURATION,
            MediaStore.Video.VideoColumns.SIZE,
            MediaStore.Video.VideoColumns._ID};

    public static final String TAG = "me.nereo.multi_image_selector.MultiImageSelectorFragment";

    private static final String KEY_TEMP_FILE = "key_temp_file";

    /**
     * 最大图片选择次数，int类型
     */
    public static final String EXTRA_SELECT_COUNT = "max_select_count";
    /**
     * 图片选择模式，int类型
     */
    public static final String EXTRA_SELECT_MODE = "select_count_mode";
    /**
     * 是否显示相机，boolean类型
     */
    public static final String EXTRA_SHOW_CAMERA = "show_camera";
    /**
     * 是否选择原图
     */
    public static final String EXTRA_SELECT_ORIGINAL = "select_original";
    /**
     * 默认选择的数据集
     */
    public static final String EXTRA_DEFAULT_SELECTED_LIST = "default_result";
    /**
     * 单选
     */
    public static final int MODE_SINGLE = 0;
    /**
     * 多选
     */
    public static final int MODE_MULTI = 1;
    // 不同loader定义
    private static final int LOADER_ALL = 0;
    private static final int LOADER_CATEGORY = 1;
    // 请求加载系统照相机
    private static final int REQUEST_CAMERA = 100;


    // 结果数据
    private ArrayList<String> resultList = new ArrayList<>();
    // 文件夹数据
    private ArrayList<Folder> mResultFolder = new ArrayList<>();

    // 图片Grid
    private GridView mGridView;
    private Callback mCallback;

    private ImageGridAdapter mImageAdapter;
    private FolderAdapter mFolderAdapter;

    private ListPopupWindow mFolderPopupWindow;

    // 类别
    private TextView mCategoryText;
    // 预览按钮
    private Button mPreviewBtn;
    //原图按钮
    private RadioButton mOriginalBtn;
    //原图容器
    private LinearLayout ll_original;
    // 底部View
    private View mPopupAnchorView;

    private int mDesireImageCount;

    private boolean hasFolderGened = false;
    private boolean mIsShowCamera = false;
    private boolean mIsOriginal = false;

    private File mTmpFile;
    private String mShowCategory;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallback = (Callback) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("The Activity must implement MultiImageSelectorFragment.Callback interface...");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_multi_image, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 选择图片数量
        mDesireImageCount = getArguments().getInt(EXTRA_SELECT_COUNT);

        // 图片选择模式
        final int mode = getArguments().getInt(EXTRA_SELECT_MODE);

        // 默认选择
        if (mode == MODE_MULTI) {
            ArrayList<String> tmp = getArguments().getStringArrayList(EXTRA_DEFAULT_SELECTED_LIST);
            if (tmp != null && tmp.size() > 0) {
                resultList = tmp;
            }
        }

        // 是否显示照相机
        mIsShowCamera = getArguments().getBoolean(EXTRA_SHOW_CAMERA, true);
        //是否获取原图
        mIsOriginal = getArguments().getBoolean(EXTRA_SELECT_ORIGINAL, false);
        mShowCategory = getArguments().getString(MultiImageSelectorActivity.SHOW_CATEGORY);
        mImageAdapter = new ImageGridAdapter(getActivity(), mIsShowCamera, 3);
        // 是否显示选择指示器
        mImageAdapter.showSelectIndicator(mode == MODE_MULTI);

        mPopupAnchorView = view.findViewById(R.id.footer);

        mCategoryText = (TextView) view.findViewById(R.id.category_btn);
        mPreviewBtn = (Button) view.findViewById(R.id.preview);
        ll_original = (LinearLayout) view.findViewById(R.id.ll_original);
        // 初始化，加载所有图片
        if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_BOTH)) {
            mCategoryText.setText(R.string.folder_all);
        } else if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_VIDEO_ONLY)) {
            mCategoryText.setText(R.string.folder_all_video);
            mPreviewBtn.setVisibility(View.GONE);
            ll_original.setVisibility(View.GONE);
        } else if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_IMG_ONLY)) {
            mCategoryText.setText(R.string.folder_all_img);
        }
        mCategoryText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mFolderPopupWindow == null) {
                    createPopupFolderList();
                }

                if (mFolderPopupWindow.isShowing()) {
                    mFolderPopupWindow.dismiss();
                } else {
                    mFolderPopupWindow.show();
                    int index = mFolderAdapter.getSelectIndex();
                    index = index == 0 ? index : index - 1;
                    mFolderPopupWindow.getListView().setSelection(index);
                }
            }
        });

        //初始化原图按钮状态
        mOriginalBtn = (RadioButton) view.findViewById(R.id.original_btn);
        ll_original.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOriginalBtn.setChecked(!mOriginalBtn.isChecked());
                if (mCallback != null) {
                    mCallback.onOriginalSelected(mOriginalBtn.isChecked());
                }
            }
        });

        // 初始化，按钮状态初始化
        if (resultList == null || resultList.size() <= 0) {
            mPreviewBtn.setText(R.string.preview);
            mPreviewBtn.setEnabled(false);
        }

        mPreviewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (resultList == null || resultList.size() == 0) {
                    return;
                }

                // TODO 预览
                if (mCallback != null) {
                    mCallback.onPreviewImage();
                }
                //预览选中的图像
                Intent intent = new Intent(getActivity(), PhotoPagerActivity.class);
                intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, 0);
                intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, resultList);
                startActivityForResult(intent, 1);
            }
        });

        mGridView = (GridView) view.findViewById(R.id.grid);
        mGridView.setAdapter(mImageAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mImageAdapter.isShowCamera()) {
                    // 如果显示照相机，则第一个Grid显示为照相机，处理特殊逻辑
                    if (i == 0) {
                        showCameraAction();
                    } else {
                        Image image = (Image) adapterView.getAdapter().getItem(i);
                        if (image.type == 1) {
                            File file = new File(image.path);
                            if (file.length() > 20 * 1024 * 1024) {
                                Toast.makeText(MultiImageSelectorFragment.this.getContext(), "选择视频大于20MB", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            toVideoDetailActivity(image);
                            return;
                        }
                        // 正常操作
                        selectImageFromGrid(image, mode);
                    }
                } else {
                    // 正常操作
                    Image image = (Image) adapterView.getAdapter().getItem(i);
                    if (image.type == 1) {
                        File file = new File(image.path);
                        if (file.length() > 20 * 1024 * 1024) {
                            Toast.makeText(MultiImageSelectorFragment.this.getContext(), "选择视频大于20MB", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        toVideoDetailActivity(image);
                        return;
                    }
                    selectImageFromGrid(image, mode);
                }
            }
        });

        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_FLING) {
                    Glide.with(view.getContext()).pauseRequests();
                } else {
                    Glide.with(view.getContext()).resumeRequests();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        mFolderAdapter = new FolderAdapter(getActivity(), mShowCategory);
    }

    private void toVideoDetailActivity(Image image) {
        Intent videoIntent = new Intent();
        videoIntent.setClassName(MultiImageSelectorFragment.this.getContext(), "com.thinksns.sociax.t4.android.video.ActivityIjkPlayer");
        videoIntent.putExtra("url", image.path);
        videoIntent.putExtra("show_ok_btn", true);
        startActivityForResult(videoIntent, PREVIEW_VIDEO);
    }

    /**
     * 创建弹出的ListView
     */
    private void createPopupFolderList() {
        Point point = ScreenUtils.getScreenSize(getActivity());
        int width = point.x;
        int height = (int) (point.y * (4.5f / 8.0f));
        FragmentActivity fragmentActivity = getActivity();
        mFolderPopupWindow = new ListPopupWindow(fragmentActivity);
        mFolderPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        mFolderPopupWindow.setAdapter(mFolderAdapter);
        mFolderPopupWindow.setContentWidth(width);
        mFolderPopupWindow.setWidth(width);
        mFolderPopupWindow.setHeight(height);
        mFolderPopupWindow.setAnchorView(mPopupAnchorView);
        mFolderPopupWindow.setModal(true);
        mFolderPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                mFolderAdapter.setSelectIndex(i);

                final int index = i;
                final AdapterView v = adapterView;

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mFolderPopupWindow.dismiss();

                        if (index == 0) {
                            loadDatas();
                            if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_BOTH)) {
                                mCategoryText.setText(R.string.folder_all);
                            } else if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_VIDEO_ONLY)) {
                                mCategoryText.setText(R.string.folder_all_video);
                            } else if (mShowCategory.equals(MultiImageSelectorActivity.SHOW_IMG_ONLY)) {
                                mCategoryText.setText(R.string.folder_all_img);
                            }
                            if (mIsShowCamera) {
                                mImageAdapter.setShowCamera(true);
                            } else {
                                mImageAdapter.setShowCamera(false);
                            }
                        } else {
                            Folder folder = (Folder) v.getAdapter().getItem(index);
                            if (null != folder) {
                                mImageAdapter.setData(folder.images);
                                mCategoryText.setText(folder.name);
                                // 设定默认选择
                                if (resultList != null && resultList.size() > 0) {
                                    mImageAdapter.setDefaultSelected(resultList);
                                }
                            }
                            mImageAdapter.setShowCamera(false);
                        }

                        // 滑动到最初始位置
                        mGridView.smoothScrollToPosition(0);
                    }
                }, 100);

            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_TEMP_FILE, mTmpFile);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (savedInstanceState != null) {
            mTmpFile = (File) savedInstanceState.getSerializable(KEY_TEMP_FILE);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadDatas();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 相机拍照完成后，返回图片路径
        if (requestCode == REQUEST_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {
                if (mTmpFile != null) {
                    if (mCallback != null) {
                        mCallback.onCameraShot(mTmpFile);
                    }
                }
            } else {
                while (mTmpFile != null && mTmpFile.exists()) {
                    boolean success = mTmpFile.delete();
                    if (success) {
                        mTmpFile = null;
                    }
                }
            }
        } else if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                List<String> photos = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST);
                if (photos != null) {
                    resultList.clear();
                    resultList.addAll(photos);
                    mImageAdapter.setDataByPath(resultList);
                    mPreviewBtn.setText(getResources().getString(R.string.preview) + "(" + resultList.size() + ")");
                }

                mIsOriginal = data.getBooleanExtra(MultiImageSelectorActivity.EXTRA_SELECT_ORIGIANL, false);
                mOriginalBtn.setChecked(mIsOriginal);
                if (mCallback != null) {
                    mCallback.onOriginalSelected(mIsOriginal);
                    //重新设置选中图像列表
                    mCallback.onPreviewSelected(photos);
                }
            }
        } else if (requestCode == PREVIEW_VIDEO) {
            if (resultCode == Activity.RESULT_OK) {
                getActivity().setResult(Activity.RESULT_OK);
                getActivity().finish();
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (mFolderPopupWindow != null) {
            if (mFolderPopupWindow.isShowing()) {
                mFolderPopupWindow.dismiss();
            }
        }
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 选择相机
     */
    private void showCameraAction() {
        // 跳转到系统照相机
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            // 设置系统相机拍照后的输出路径
            // 创建临时文件
            try {
                mTmpFile = FileUtils.createTmpFile(getActivity());
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (mTmpFile != null && mTmpFile.exists()) {
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
                startActivityForResult(cameraIntent, REQUEST_CAMERA);
            } else {
                Toast.makeText(getActivity(), "图片错误", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getActivity(), R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 选择图片操作
     *
     * @param image
     */
    private void selectImageFromGrid(Image image, int mode) {
        if (image != null) {
            // 多选模式
            if (mode == MODE_MULTI) {
                if (resultList.contains(image.path)) {
                    resultList.remove(image.path);
                    if (resultList.size() != 0) {
                        mPreviewBtn.setEnabled(true);
                        mPreviewBtn.setText(getResources().getString(R.string.preview) + "(" + resultList.size() + ")");
                    } else {
                        mPreviewBtn.setEnabled(false);
                        mPreviewBtn.setText(R.string.preview);
                    }
                    if (mCallback != null) {
                        mCallback.onImageUnselected(image.path);
                    }
                } else {
                    // 判断选择数量问题
                    if (mDesireImageCount == resultList.size()) {
                        Toast.makeText(getActivity(), R.string.msg_amount_limit, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    resultList.add(image.path);
                    mPreviewBtn.setEnabled(true);
                    mPreviewBtn.setText(getResources().getString(R.string.preview) + "(" + resultList.size() + ")");
                    if (mCallback != null) {
                        mCallback.onImageSelected(image.path);
                    }
                }
                mImageAdapter.select(image);
            } else if (mode == MODE_SINGLE) {
                // 单选模式
                if (mCallback != null) {
                    mCallback.onSingleImageSelected(image.path);
                }
            }
        }
    }

    private void loadDatas() {
        List<Image> list = new ArrayList<Image>();
        if (mShowCategory != null && mShowCategory.equals(MultiImageSelectorActivity.SHOW_VIDEO_ONLY)) {
            list.addAll(loadMediaDatas(MultiImageSelectorActivity.SHOW_VIDEO_ONLY));
        } else if (mShowCategory != null && mShowCategory.equals(MultiImageSelectorActivity.SHOW_IMG_ONLY)) {
            list.addAll(loadMediaDatas(MultiImageSelectorActivity.SHOW_IMG_ONLY));
        } else if (mShowCategory != null && mShowCategory.equals(MultiImageSelectorActivity.SHOW_BOTH)) {
            list.addAll(loadMediaDatas(MultiImageSelectorActivity.SHOW_IMG_ONLY));
            list.addAll(loadMediaDatas(MultiImageSelectorActivity.SHOW_VIDEO_ONLY));
        }
        Collections.sort(list);
        mImageAdapter.setData(list);
        // 设定默认选择
        if (resultList != null && resultList.size() > 0) {
            mImageAdapter.setDefaultSelected(resultList);
        }

        if (!hasFolderGened) {
            mFolderAdapter.setData(mResultFolder);
            hasFolderGened = true;
        }
    }

    private List<Image> loadMediaDatas(String category) {
        Cursor cursor;
        if (category != null && category.equals(MultiImageSelectorActivity.SHOW_VIDEO_ONLY)) {
            cursor = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    VIDEO_PROJECTION,
                    VIDEO_PROJECTION[5] + ">0 AND " + VIDEO_PROJECTION[3] + "=? AND " + VIDEO_PROJECTION[0] + " like '%.mp4'",
                    new String[]{"video/mp4"},
                    null);
        } else {
            cursor = getContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    IMAGE_PROJECTION,
                    IMAGE_PROJECTION[4] + ">0 AND " + IMAGE_PROJECTION[3] + "=? OR " + IMAGE_PROJECTION[3] + "=? ",
                    new String[]{"image/jpeg", "image/png"},
                    null);
        }
        return getCursorDatas(cursor, category);
    }

    private List<Image> getCursorDatas(Cursor cursor, String mShowCategory) {
        List<Image> images = new ArrayList<Image>();
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                do {
                    String path = null;
                    String name = null;
                    long dateTime = 0;
                    long duration = 0;
                    int type = 0;
                    if (mShowCategory != null && mShowCategory.equals(MultiImageSelectorActivity.SHOW_IMG_ONLY)) {
                        path = cursor.getString(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[0]));
                        name = cursor.getString(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[1]));
                        dateTime = cursor.getLong(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[2]));
                        type = 0;
                    } else if (mShowCategory != null && mShowCategory.equals(MultiImageSelectorActivity.SHOW_VIDEO_ONLY)) {
                        path = cursor.getString(cursor.getColumnIndexOrThrow(VIDEO_PROJECTION[0]));
                        name = cursor.getString(cursor.getColumnIndexOrThrow(VIDEO_PROJECTION[1]));
                        dateTime = cursor.getLong(cursor.getColumnIndexOrThrow(VIDEO_PROJECTION[2]));
                        duration = cursor.getLong(cursor.getColumnIndexOrThrow(VIDEO_PROJECTION[4]));
                        type = 1;
                    }

                    Image image = null;
                    if (fileExist(path)) {
                        image = new Image(path, name, duration, type, dateTime);
                        images.add(image);
                    }
                    if (!hasFolderGened) {
                        // 获取文件夹名称
                        File folderFile = new File(path).getParentFile();
                        if (folderFile != null && folderFile.exists()) {
                            String fp = folderFile.getAbsolutePath();
                            Folder f = getFolderByPath(fp);
                            if (f == null) {
                                Folder folder = new Folder();
                                folder.name = folderFile.getName();
                                folder.path = fp;
                                folder.cover = image;
                                List<Image> imageList = new ArrayList<>();
                                imageList.add(image);
                                folder.images = imageList;
                                mResultFolder.add(folder);
                            } else {
                                f.images.add(image);
                            }
                        }
                    }

                } while (cursor.moveToNext());
            }
        }
        return images;
    }

    private boolean fileExist(String path) {
        if (!TextUtils.isEmpty(path)) {
            return new File(path).exists();
        }
        return false;
    }

    private Folder getFolderByPath(String path) {
        if (mResultFolder != null) {
            for (Folder folder : mResultFolder) {
                if (TextUtils.equals(folder.path, path)) {
                    return folder;
                }
            }
        }
        return null;
    }

    /**
     * 回调接口
     */
    public interface Callback {
        void onSingleImageSelected(String path);

        void onImageSelected(String path);

        void onImageUnselected(String path);

        void onCameraShot(File imageFile);

        void onOriginalSelected(boolean selected);  //选择原图

        void onPreviewImage();      //预览选择的图片

        void onPreviewSelected(List<String> paths);

    }
}
